import {
  createMemoryHistory,
  createRouter,
  createWebHistory,
} from 'vue-router'

const routes = [
  {
    path: '/login',
    component: () => import('@/views/Login.vue'),
  },
  {
    path: '/register',
    component: () => import('../views/Register.vue'),
  },
  {
    path: '/',
    redirect: '/home',
  },


  // 网站
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/LayOut.vue'),
    children: [
      {
        path: '/home',
        name: '/home',
        component: () => import('@/views/HomeView.vue'),
        meta: {
          title: '首页'
        }
      },
      {
        path: '/privacypolicy',
        name: '隱私權政策',
        component: () => import('@/views/PrivacyPolicy.vue'),
        meta: {
          title: '隱私權政策',
        },
      },
      {
        path: '/tos',
        name: '服務條款',
        component: () => import('@/views/Tos.vue'),
      },
      {
        path: '/charts',
        name: '排行榜',
        component: () => import('@/views/Charts.vue'),
      },
    ]
  },
  {
    path: '/updateWrod',
    name: '變更密码',
    component: () =>
      import(
        '../components/AccountManagement/PasswordUpdate.vue'
      ),
  },
  {
    path: '/ForgetWord',
    name: '忘记密码',
    component: () =>
      import(
        '../components/AccountManagement/ForgetWord.vue'
      ),
  },

  {
    path: '/search',
    name: '搜索结果',
    component: () =>
      import(
        '../views/Search.vue'
      ),
  },
  {
    path: '/announce',
    name: '活动公告',
    component: () =>
      import(
        '../views/MyAlerts.vue'
      ),
  },
  {
    path: '/announce/:id',
    name: 'announce_detail',
    component: () =>
      import(
        '../views/NotificationDetails.vue'
      ),
  },
  {
    path: '/reply',
    name: '我收到的回應',
    component: () =>
      import(
        '../views/Reply.vue'
      ),
  },
  {
    path: '/collection',
    name: '我的收藏',
    component: () =>
      import(
        '../views/MyCollection.vue'
      ),
  },
  {
    path: '/personal',
    name: '個人设定',
    component: () =>
      import(
        '../views/PersonalSettings.vue'
      ),
  },
  {
    path: '/book',
    name: '书籍',
    component: () =>
      import(
        '../views/Book.vue'
      ),
  },
  {
    path: '/creation_center',
    redirect:
      '/creation_center/index',
    name: '创作中心',
    component: () =>
      import(
        '../views/CreationCenter.vue'
      ),
    children: [
      {
        path: '/creation_center/index',
        name: '主页',
        component: () =>
          import(
            '../components/CreationIndex.vue'
          ),
      },
      {
        path: '/creation_center/myWork',
        name: '我的作品',
        component: () =>
          import(
            '../components/MyWork.vue'
          ),
      },
      {
        path: '/creation_center/editBook',
        name: '编辑作品',
        component: () =>
          import(
            '../components/EditWorks.vue'
          ),
      },
      {
        path: '/creation_center/bookSetting',
        name: '作品设定',
        component: () =>
          import(
            '../components/BookSetting.vue'
          ),
      },
      {
        path: '/creation_center/chapterSort',
        name: '章节排序',
        component: () =>
          import(
            '../components/ChapterSort.vue'
          ),
      },

      {
        path: '/chapterEditContent',
        name: '章节设定',
        component: () => import('../views/chapterEditContent.vue'),
      },
    ],
  },
  {
    path: '/chapterEdit',
    name: '章节设定',
    component: () => import('../views/chapterEdit.vue'),
  },
  {
    path: '/chapter',
    name: '篇章',
    component: () =>
      import(
        '../views/Chapter.vue'
      ),
  },

  {
    path: '/user',
    name: '用户',
    component: () =>
      import(
        '../views/User.vue'
      ),
  },
  {
    path: '/register/process1',
    name: '注册',
    component: () =>
      import(
        '../components/RegProcess/Process1.vue'
      ),
  },
  {
    path: '/register/process3',
    name: '注册',
    component: () =>
      import(
        '../components/RegProcess/Process3.vue'
      ),
  },
  {
    path: '/user/account',
    name: '账号管理',
    component: () => import('../components/AccountManagement/AccountMgt.vue'),
  },

  {
    path: '/update/emails',
    name: '變更 Email',
    component: () => import('../components/AccountManagement/EmailUpdate.vue'),
  }

]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// let isgetInfo = false
//全局前置路由守卫
router.beforeEach(async (to, from, next) => {
  if (to.meta.title) {
    document.title =
      to.meta.title
  }
  //获取token
  // const tokenStr = window.sessionStorage.getItem('token')
  // //如果未登录跳转到登录页面
  // if (!tokenStr && to.path !== '/login') return next('/login')
  // //如果已登录避免重复登录
  // if (tokenStr && to.path == '/login') {
  //   return next({ path: from.path ? from.path : '/' })
  // }
  // //如果用户登录成功，调用Vuex方法，存储用户信息
  // if (tokenStr && !isgetInfo) {
  //   const res = await store.dispatch('getUserInfo')
  //   isgetInfo = true
  // }
  next()
})

export default router
